#include "NetworkByteArray.h"

NetworkByteArray::NetworkByteArray() : Array<char>()
{}


NetworkByteArray::~NetworkByteArray()
{

}

NetworkByteArray::NetworkByteArray( const NetworkByteArray& copy)
{

	m_initSize = copy.m_initSize;
	m_increaseStep = copy.m_increaseStep;
	m_dataSize = copy.m_dataSize;
	m_arraySize = copy.m_arraySize;
	m_data = new char [ m_arraySize ];
	memcpy( (void * ) m_data, ( void * ) copy.m_data, m_arraySize * sizeof( char ) );
}

unsigned char NetworkByteArray::getUnsignedChar( int position )
{
	return ( unsigned char ) m_data[ position ];
}

unsigned int NetworkByteArray::getUnsignedInt( int position )
{
	char data[4];	// Unsigned int = 32 Bits

	data[ 0 ] = m_data[ position ];
	data[ 1 ] = m_data[ position + 1 ];
	data[ 2 ] = m_data[ position + 2 ];
	data[ 3 ] = m_data[ position + 3 ];

	return *( ( unsigned int* ) data );
}

float NetworkByteArray::getFloat( int position )
{
	char data[4];	// Float = 32 Bits

	data[ 0 ] = m_data[ position ];
	data[ 1 ] = m_data[ position + 1 ];
	data[ 2 ] = m_data[ position + 2 ];
	data[ 3 ] = m_data[ position + 3 ];

	return *( ( float* ) data );
}

void NetworkByteArray::pushUnsignedChar( unsigned char uc )
{
	push( (char)uc );
}

void NetworkByteArray::pushUnsignedInt( unsigned int ui )
{
	char data[4];	// Float = 32 Bits

	*( (unsigned int*) data) = ui;

	push( data[ 0 ]  );
	push( data[ 1 ]  );
	push( data[ 2 ]  );
	push( data[ 3 ]  );
}

void NetworkByteArray::pushFloat( float f )
{
	char data[4];	// Float = 32 Bits

	*( (float*) data) = f;

	push( data[ 0 ]  );
	push( data[ 1 ]  );
	push( data[ 2 ]  );
	push( data[ 3 ]  );
}

void NetworkByteArray::insertDataSize()
{
	char data[4];	// Float = 32 Bits

	*( (unsigned int*) data) = m_dataSize;

	insertAt( data[ 0 ] , 0 );
	insertAt( data[ 1 ] , 1 );
	insertAt( data[ 2 ] , 2 );
	insertAt( data[ 3 ] , 3 );
}

unsigned int NetworkByteArray::getDataSize()
{
	return getUnsignedInt( 0 );
}

void NetworkByteArray::removeDataSize()
{
	// Remove the 4 bytes of the data size
	remove( 0 );
	remove( 0 );
	remove( 0 );
	remove( 0 );
}

string NetworkByteArray::getString( int position, int lenght )
{
	string returnStr = "";
	for( int i = 0; i < lenght; ++i )
	{
		returnStr += m_data[ position + i  ];
	}
	return returnStr;
}

void NetworkByteArray::pushString( std::string s )
{
	for( unsigned int i = 0; i < s.size(); ++i )
	{
		push( s[ i ] );
	}
}
